package com.lsw.jgyf.ui.activity;

import com.lsw.dialog.DialogProgress;
import com.lsw.jgyf.httpapi.HttpApi;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.ui.base.presenter.BasePresenter;
import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.LogUtils;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2018/7/10 0010.
 */

public class PhoneLoginPresenter extends BasePresenter<PhoneLoginContract.View> implements PhoneLoginContract.Presenter {
    @Inject
    HttpApi httpApi;

    @Inject
    public PhoneLoginPresenter() {

    }

    @Override
    public void getData() {
        mView.setData();
    }

    @Override
    public void Login(String phone, String code) {
        if (StringUtil.isNullOrEmpty(phone)) {
            ToastUtil.showToast(mContext, "请输入手机号！");
            return;
        }
        if (!StringUtil.checkPhoneNum(phone)) {
            ToastUtil.showToast(mContext, "请输入正确的手机号！");
            return;
        }
        if (StringUtil.isNullOrEmpty(code)) {
            ToastUtil.showToast(mContext, "请输入验证码！");
            return;
        }
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .loginphone(phone, code)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        ToastUtil.showToast(mContext, "登录成功");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.ID, baseBean.getData().getId() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.USERNAME, baseBean.getData().getUsername());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.MOBILE, baseBean.getData().getMobile());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.STORE_ID, baseBean.getData().getStore_id() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.IMG_ID, baseBean.getData().getImg_id() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.TYPE, baseBean.getData().getType() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.STATUS, baseBean.getData().getStatus() + "");
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.CREATE_TIME, baseBean.getData().getCreate_time());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.UPDATE_TIME, baseBean.getData().getUpdate_time());
                        ShareUserInfoUtil.getInstance(mContext).setString(ShareUserInfoUtil.HEADPHOTO, baseBean.getData().getHead_url());
                        mView.loginSucceed();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage().toString());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void sendCode(String phone, String type) {
        if (StringUtil.isNullOrEmpty(phone)) {
            ToastUtil.showToast(mContext, "请输入手机号！");
            return;
        }
        if (!StringUtil.checkPhoneNum(phone)) {
            ToastUtil.showToast(mContext, "请输入正确的手机号！");
            return;
        }
        DialogProgress progress = new DialogProgress(mContext);
        progress.show();
        Disposable disposable = httpApi
                .SendCode(phone, type)
                .subscribe(baseBean -> {
                    progress.dismiss();
                    if (baseBean.getStatus() == Constants.CODE_SUCESS) {
                        ToastUtil.showToast(mContext, baseBean.getMessage().toString());
                        mView.sendSucess();
                    } else {
                        ToastUtil.showToast(mContext, baseBean.getMessage().toString());
                    }
                }, throwable -> {
                    progress.dismiss();
                    LogUtils.e("TAG_Enroll", throwable.getMessage());
                });
        mCompositeDisposable.add(disposable);
    }
}
